<?php

/*
*/

class Setting_model extends CI_Model
{

	public $error;
	private $settings;
	private $type_id;

	public function __construct()
	{
		parent::__construct();

		//选择数据库环境
		$this->load->library('test_database');
		$this->test_database->test();
		$this->load->database($this->test_database->get_db());

		//载入模型
		$this->load->model('Inbound_Type_model');
		$this->load->model('Outbound_Type_model');
		
		//初始化模型
		$this->Inbound_Type_model->init();
		$this->Outbound_Type_model->init();

		//初始化参数
		$this->error = array();
		$this->settings = array('basic'=>array(), 'type'=>array());
	}

	public function get_settings()
	{
		//获得所有入库类型
		$this->Inbound_Type_model->get_all();
		$this->settings['type']['inbound_types'] = $this->Inbound_Type_model->inbound_types();
		//获得所有出库类型
		$this->Outbound_Type_model->get_all();
		$this->settings['type']['outbound_types'] = $this->Outbound_Type_model->outbound_types();
		return TRUE;
	}

	//添加类型
	public function add_type()
	{
		if ($type = $this->input->post('type'))
		{
			try {
				$add_type = 'add_' . $type;
				if ($this->$add_type())
				{
					return TRUE;
				}
			} catch (Exception $e) {
				$this->error = 'SYS_ERR';
			}
		}
		else
		{
			$this->error = 'SYS_ERR';
		}
		return FALSE;
	}

	//添加入库类型
	private function add_inbound_type()
	{
		if ($this->Inbound_Type_model->create())
		{
			$this->type_id = $this->Inbound_Type_model->inbound_type_id();
			return TRUE;
		}
		else
		{
			$this->error = $this->Inbound_Type_model->error;
		}
		return FALSE;
	}

	//添加出库类型
	private function add_outbound_type()
	{
		if ($this->Outbound_Type_model->create())
		{
			$this->type_id = $this->Outbound_Type_model->outbound_type_id();
			return TRUE;
		}
		else
		{
			$this->error = $this->Outbound_Type_model->error;
		}
		return FALSE;
	}

	public function settings()
	{
		return $this->settings;
	}

	public function type_id()
	{
		return $this->type_id;
	}

}